package com.example.weather_news.fragment;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.weather_news.R;
import com.example.weather_news.util.ShareUtils;


public class Fragment_Mine extends BaseFragment {
    private ImageView icon_avatar;
    private TextView tv_change;
    private TextView tv_alarm;
    private TextView tv_username;
    private TextView tv_notes;
    protected static final int CHOOSE_PICTURE = 0;
    private static final int CROP_SNALL_PICTURE = 1;
    protected static Uri tempUri;

    private String name;

    @Override
    public void initView() {
        icon_avatar = findView(R.id.icon_avatar);
        tv_change = findView(R.id.tv_change);
        tv_alarm = findView(R.id.tv_alarm);
        tv_username = findView(R.id.tv_username);
        tv_notes = findView(R.id.tv_notes);

        setOnClick(tv_change, tv_username, icon_avatar, tv_alarm, tv_notes);
    }

    @Override
    public void initData() {
        setStatusBarColor();

//        //设置头像
//        String path=ShareUtils.getString(getActivity(),ShareUtils.IMAGE_PATH,null);
//        if (path != null) {
//            Uri uri = Uri.fromFile(new File(path));
//            icon_avatar.setImageURI(uri);
//        }
    }

    @Override
    public void onStart() {
        super.onStart();

        name = ShareUtils.getString(getActivity(), ShareUtils.USER_NAME, "点击登录");
        tv_username.setText(name);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.icon_avatar:
//                startActivity(new Intent(getActivity(), EnterActivity.class));
                break;
            case R.id.tv_username:
//                startActivityForResult(new Intent(getActivity(), EnterActivity.class),
//                        CROP_SNALL_PICTURE);
                break;
            case R.id.tv_change:
                if (TextUtils.equals("点击登录", name)) {
                    Toast.makeText(getActivity(), "请先登录再更换头像", Toast.LENGTH_SHORT).show();
                    return;
                }
                showChoosePicDialog();
                break;
            case R.id.tv_alarm:
//                startActivity(new Intent(getActivity(), AlarmActivity.class));
                break;
            case R.id.tv_notes:
//                startActivity(new Intent(Fragment_Mine.this.getContext(), MemoActivity.class));
                break;
        }
        getActivity().
                overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
    }

    protected void showChoosePicDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(Fragment_Mine.this.getContext());
        builder.setTitle("设置头像");
        String[] items = new String[]{"选择本地照片"};
        builder.setNeutralButton("取消", null);
        builder.setItems(items, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                switch (which) {
                    case CHOOSE_PICTURE:
                        Intent openAlbumIntent = new Intent(Intent.ACTION_GET_CONTENT);
                        openAlbumIntent.setType("image/*");
                        startActivityForResult(openAlbumIntent, CHOOSE_PICTURE);
                        break;
                }
            }
        });
        builder.create().show();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case CHOOSE_PICTURE:
                startPhotoZoom(data.getData());
                break;
            case CROP_SNALL_PICTURE:
                if (data != null) {
                    setIconToView(data);
                }
                break;
        }
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            setStatusBarColor();
        }
    }

    //裁剪图片方法实现
    protected void startPhotoZoom(Uri uri) {
        if (uri == null) {
            Log.e("tag", "The uri is't exist");
        }
        tempUri = uri;
        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(uri, "image/*");
        //设置裁剪
        intent.putExtra("crop", "true");
        // aspectX aspectY 是宽高的比例
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);
        // outputX outputY 是裁剪图片宽高
        intent.putExtra("outputX", 150);
        intent.putExtra("outputY", 150);
        intent.putExtra("return-data", true);
        startActivityForResult(intent, CROP_SNALL_PICTURE);
    }



    //保存裁剪之后的图片数据
    protected void setIconToView(Intent data) {
        Bundle extras = data.getExtras();
        if (extras != null) {
            Log.e("extras", "extras != null");
            Bitmap photo = extras.getParcelable("data");
//            photo = IconUtils.toRoundBitmap(photo, tempUri);
            icon_avatar.setImageBitmap(photo);
//            uploadPic(photo);
        }
    }


    //设置沉浸栏颜色
    private void setStatusBarColor() {
        this.getActivity().getWindow().setStatusBarColor(getResources().getColor(R.color
                .blue_check));
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_mine;
    }

    private void uploadPic(Bitmap bitmap) {
        // 上传至服务器
        // ... 可以在这里把Bitmap转换成file，然后得到file的url，做文件上传操
        // bitmap是没有做个圆形处理的，但已经被裁剪了
//        String imagePath = IconUtils.savePhoto(bitmap, Environment
//                .getExternalStorageDirectory().getAbsolutePath(), String
//                .valueOf(System.currentTimeMillis()));
        //保存路径
//        SharedPreferences sharedPreferences = Fragment_Mine.this.getContext()
//                .getSharedPreferences("imagePath", MODE_PRIVATE);
//        SharedPreferences.Editor editor = sharedPreferences.edit();
//        editor.putString("path", imagePath.toString());
//        editor.apply();

//        ShareUtils.setString(getActivity(),ShareUtils.IMAGE_PATH,imagePath.toString());
    }
}
